package cn.tradewin.pmp.pages.security;

import java.util.List;

import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.BeanModelSource;
import org.slf4j.Logger;

import cn.tradewin.pmp.persist.model.Contract;
import cn.tradewin.pmp.persist.model.ContractItem;

public class AddContractDetail extends AddContract {

	@Property
	private Contract contract;
	
	@Property
	private ContractItem addItem;

	@Property
	private ContractItem current;
	
	@InjectPage
	private AddContractMain back;
	
	
	@InjectPage
	private AddContractConfirm next;
	
	@Inject
	private BeanModelSource beanModelSource;
	
	@Property
	private BeanModel<ContractItem> beanModel;

	@Inject
	private Messages messages;
	

	@Inject
	Logger log;
	
	@Log
	void setupRender() {
		beanModel = beanModelSource.createDisplayModel(ContractItem.class, messages);
		beanModel.add("delete", null);
		beanModel.get("delete").label("");
		beanModel.include("itemEntry","startDate","endDate","itemAmount","itemUnit","itemPrice","itemDetail", "delete");
	}

	@Log
	void onPrepareForRender() {

	}

	@Log
	void onPrepare() {
		if (contract == null) {
			contract = this.restoreContractFromConversation();
		}
		if (addItem == null) {
			addItem = new ContractItem();
		}
	}

	@Log
	void prepareForRender() {

	}

	// void onValidateFromForm() {
	// if (form.getHasErrors()) {
	// // We get here only if a server-side validator detected an error.
	// return;
	// }
	// this.savePersonToConversation(person);
	//
	// }
	//
	
	
	Object onSuccess() {
		List<ContractItem> items = contract.getItems(); 
		Integer itemNo = 0;
		int size = items.size();
		if (size > 0) {
			itemNo = items.get(size-1).getItemNo()+1;
		}
		ContractItem add = new ContractItem();
		add.setItemNo(itemNo);
		add.setItemEntry(addItem.getItemEntry());
		add.setStartDate(addItem.getStartDate());
		add.setEndDate(addItem.getEndDate());
		add.setItemAmount(addItem.getItemAmount());
		add.setItemUnit(addItem.getItemUnit());
		add.setItemPrice(addItem.getItemPrice());
		add.setItemDetail(String.valueOf(addItem.getItemAmount().multiply(addItem.getItemPrice())));
		contract.getItems().add(size, add);
		return null;
	}

	Object onNext() {
		next.set(this.getConversationId());
		return next;
	}
	
	Object onBack() {
		back.set(this.getConversationId());
		return back;
	}
	
	@Log
	void onDelete(Integer itemNo) {
		if (contract == null) {
			contract = this.restoreContractFromConversation();
		}
		for (ContractItem item : contract.getItems()) {
			if (item.getItemNo().equals(itemNo)) {
				contract.getItems().remove(item);
				break;
			}
		}
	}

	Object onQuit() {
		endConversation();
		return null;
	}
}
